package com.ruoyi.product.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.product.domain.SupplierProduct;
import com.ruoyi.product.mapper.SupplierProductMapper;
import com.ruoyi.product.service.ISupplierProductService;
import com.ruoyi.medication.domain.Medication;
import com.ruoyi.medication.service.IMedicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 药品供货情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class SupplierProductServiceImpl implements ISupplierProductService {
    @Autowired
    private SupplierProductMapper supplierProductMapper;

    @Autowired
    private IMedicationService medicationService;

    /**
     * 查询药品供货情况
     * 
     * @param id 药品供货情况主键
     * @return 药品供货情况
     */
    @Override
    public SupplierProduct selectSupplierProductById(Long id) {
        return supplierProductMapper.selectSupplierProductById(id);
    }

    /**
     * 查询药品供货情况列表
     * 
     * @param supplierProduct 药品供货情况
     * @return 药品供货情况
     */
    @Override
    public List<SupplierProduct> selectSupplierProductList(SupplierProduct supplierProduct) {
        return supplierProductMapper.selectSupplierProductList(supplierProduct);
    }

    /**
     * 根据供应商ID和药品ID查询供货情况
     *
     * @param supplierId   供应商ID
     * @param medicationId 药品ID
     * @return 供货情况
     */
    @Override
    public SupplierProduct selectSupplierProductBySupplierAndMedication(Long supplierId, Long medicationId) {
        // 1. 通过medicationId查询药品信息
        Medication medication = medicationService.selectMedicationById(medicationId);
        if (medication == null) {
            return null;
        }

        // 2. 使用药品编号和供应商ID查询供货情况
        SupplierProduct query = new SupplierProduct();
        query.setSupplierId(supplierId);
        query.setProductCode(medication.getNumber());
        List<SupplierProduct> products = supplierProductMapper.selectSupplierProductList(query);
        return products != null && !products.isEmpty() ? products.get(0) : null;
    }

    /**
     * 新增药品供货情况
     * 
     * @param supplierProduct 药品供货情况
     * @return 结果
     */
    @Override
    public int insertSupplierProduct(SupplierProduct supplierProduct) {
        supplierProduct.setCreateTime(DateUtils.getNowDate());
        supplierProduct.setUpdateTime(DateUtils.getNowDate());
        return supplierProductMapper.insertSupplierProduct(supplierProduct);
    }

    /**
     * 修改药品供货情况
     * 
     * @param supplierProduct 药品供货情况
     * @return 结果
     */
    @Override
    public int updateSupplierProduct(SupplierProduct supplierProduct) {
        supplierProduct.setUpdateTime(DateUtils.getNowDate());
        return supplierProductMapper.updateSupplierProduct(supplierProduct);
    }

    /**
     * 批量删除药品供货情况
     * 
     * @param ids 需要删除的药品供货情况主键
     * @return 结果
     */
    @Override
    public int deleteSupplierProductByIds(Long[] ids) {
        return supplierProductMapper.deleteSupplierProductByIds(ids);
    }

    /**
     * 删除药品供货情况信息
     * 
     * @param id 药品供货情况主键
     * @return 结果
     */
    @Override
    public int deleteSupplierProductById(Long id) {
        return supplierProductMapper.deleteSupplierProductById(id);
    }
}
